<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>购买</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/aui-tab.js"></script>
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
    <script type="text/javascript">
        function closeWin() {
            api.closeWin({});
        };
    </script>
</head>

<body>
    <!-- 头部 -->
    <header class="aui-bar aui-bar-nav pos">
        <a class="aui-pull-left aui-btn" onclick="closeWin()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">购买</div>
    </header>
    <div class="header-d"></div>
    <style>
        body,
        html {
            min-height: calc(100% - 0);
        }
    </style>
    <div class="licai-buy">
        <dl>
            <dt>投资金额：</dt>
            <dd>
                <h1 onclick="reducemoney();" id="jian_money">-</h1><i>￥</i><span id="money"></span>
                <h1 onclick="addmoney();" id="add_money">+</h1></dd>
            </dl>
            <dl>
                <dt></dt>
                <dd>
                    <b class="active" onclick="sel_times(this);">X1</b>
                    <b onclick="sel_times(this);">X5</b>
                    <b onclick="sel_times(this);">X10</b>
                </dd>
            </dl>
            <dl>
                <dt>使用红包：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <div id="hongbaoList"></div>
                        <!-- <label><input class="aui-radio" type="checkbox" name="hongbao" checked onclick="getValue(this.name);"> 这是一个列表项</label> -->
                        <!-- <label><input class="aui-radio" type="checkbox" name="hongbao" onclick="getValue(this.name);"> 这是一个列表项</label> -->
                        <span>超出部分不返还</span>
                    </div>
                </dd>
            </dl>
            <dl>
                <dt>加息券：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <div id="jiaxijuanList"></div>
                        <!-- <label><input class="aui-radio" type="radio" name="jiaxijuan" checked onclick="getValue(this.name);"> 这是一个列表项</label> -->
                        <!-- <label><input class="aui-radio" type="radio" name="jiaxijuan" onclick="getValue(this.name);"> 这是一个列表项</label> -->
                        <!-- <span>超出部分不返还</span> -->
                    </div>
                </dd>
            </dl>
            <dl>
                <dt>获利方式：</dt>
                <dd>
                    <div class="aui-list-item-inner">
                        <label><input class="aui-radio" type="radio" name="huolifangshi" checked onclick="selHuolifangshi(this.value);" value="1"> 期末获利<i style="display:inline-block; float:right; font-style:normal; color:#00744b;" id="qimo"></i></label>
                        <label id="fenqihuoli"><input class="aui-radio" type="radio" name="huolifangshi" onclick="selHuolifangshi(this.value);" value="2"> 分期获利<i style="display:inline-block; float:right; font-style:normal; color:#00744b;" id="fenqi"></i></label>
                        <!-- <span>超出部分不返还</span> -->
                    </div>
                </dd>
            </dl>
        <!-- <dl>
            <dt>支付密码：</dt>
            <dd>
                <input type="password">
            </dd>
        </dl> -->
        <dl>
            <dt>预期收益：</dt>
            <dd id="shouyi"></dd>
        </dl>
        <dl>
            <dt>实付金额：</dt>
            <dd id="shifu"></dd>
        </dl>
        <div class="licai-buy-d">
            <div class="aui-clearfix"></div>
            <span onclick="xieyi1();">《借款协议》</span>
            <span onclick="xieyi2();">《委托授权书》</span>
            <span onclick="xieyi3();">《风险揭示书》</span>
            <span onclick="xieyi4();">《金可贷--电子签名数字证书用户申请确认函》</span>
        </div>
    </div>
    <div class="licai-buy-btn">
        <div class="aui-col-xs-6" onclick="buy();">购买</div>
        <div class="aui-col-xs-6" onclick="cal();">取消</div>
    </div>
</body>

<script type="text/template" id="template">
    {{~it:value:index}}
    <label><input class="aui-radio" type="checkbox" name="hongbao" onclick="selHongbao();" value="{{=value.money}}" hongbaoID="{{=value.id}}"> {{? value.ecv_type_id == 9 }}
        注册红包
        {{?? value.ecv_type_id == 10}}
        投资红包
        {{?? value.ecv_type_id == 297}}
        资金托管红包
        {{?? value.ecv_type_id == 298}}
        奖励红包
        {{?? value.ecv_type_id == 301}}
        充值红包
        {{?? value.ecv_type_id == 362}}
        绑卡红包
        {{?? value.ecv_type_id == 363}}
        哈尔滨银行绑卡红包
        {{?}}
        {{=value.money}}元
    </label> {{~}}
</script>

<script type="text/template" id="template2">
    {{~it:value:index}}
    <label><input class="aui-radio jxq" type="radio" name="jiaxijuan" onclick="selJiaxijuan(this);" value="{{=value.rate}}" jiaxijuanID="{{=value.id}}">
        额度加息券(年化) {{=value.rate}}%
    </label> {{~}}
</script>

<script type="text/javascript">
    var true_money = '';
    var ex_rate = 0;
    var free_money = 0;
    var times = 1;

    apiready = function() {
        borrow_amount = api.pageParam.borrow_amount;// 可投金额
        min_money_format_num = api.pageParam.min_money_format_num;// 起投金额
        is_zhitou = api.pageParam.is_zhitou;// 利率(期末获利)
        rate = api.pageParam.rate;// 利率(期末获利)
        rate_month = api.pageParam.rate_month;// 利率(分期获利)
        

        if (is_zhitou) {
            var qimo = $api.byId('qimo');
            qimo.innerHTML = rate+'%';
            var fenqi = $api.byId('fenqi');
            fenqi.innerHTML = rate_month+'%';
        }

        time_limit = api.pageParam.time_limit;// 期限

        if (is_zhitou) {
            // 智投分类
            if (time_limit == 3) {
                // 期限为3
                var fenqihuoli = $api.byId('fenqihuoli');
                fenqihuoli.style.display = 'none';
            }
        }

        max_loan_money = api.pageParam.max_loan_money;
        id = api.pageParam.id;
        add_money = api.pageParam.add_money;// 加减金额数

        updateData();
        updateData2();

        // if (max_loan_money==null) {
        //     alert('11');
        // }else {
        //     alert('22');
        // }

        true_money = min_money_format_num;

        $('#money').text(outputmoney(min_money_format_num));

        changeShouyi(min_money_format_num);
        changeShifu();
    }

    function sel_times(i) {
        $("b").each(function(){
            $(this).removeClass("active");
        });
        $(i).addClass("active");
        times = $(i).text().substring(1);
    }

    function changeShouyi(money) {

        var radio = document.getElementsByName('huolifangshi');
        for (i = 0; i < radio.length; i++) {
            if (radio[i].checked) {
                var type = radio[i].value;
            }
        }

        all_rate = parseFloat(rate) + parseFloat(ex_rate);

        if (type == 1) {
            $('#shouyi').text('￥' + outputmoney(parseFloat(money) * parseFloat(parseFloat(all_rate).toFixed(2)) / 100 * parseFloat(time_limit) / 12));
        }
        if (type == 2) {
            $('#shouyi').text('￥' + outputmoney(parseFloat(money) * parseFloat(parseFloat(all_rate).toFixed(2)) / 100 * parseFloat(time_limit) / 12 / parseFloat(time_limit)));
        }
    }

    function changeShifu() {

        var money = parseInt(true_money) - parseInt(free_money);

        if (money < 0) {
            money = 0;
        }

        $('#shifu').text('￥' + outputmoney(money));
    }

    function selHongbao() {

        free_money = 0;

        $("input[name='hongbao']:checkbox").each(function() {

            if ($(this).is(":checked")) {
                free_money = parseInt(free_money) + parseInt($(this).attr('value'));
            }
        });

        changeShifu();
    }

    var jiaxijuanID = '';

    function selJiaxijuan(obj) {

        // var radio = document.getElementsByName('jiaxijuan');
        // for (i=0; i<radio.length; i++) {
        //     if (radio[i].checked) {
        //         ex_rate = radio[i].value;
        //     }
        // }

        if (jiaxijuanID == $(obj).attr('jiaxijuanID')) {
            var html = $(obj).parent().html();
            $(obj).parent().html(html);
            jiaxijuanID = '';
        } else {
            jiaxijuanID = $(obj).attr('jiaxijuanID');
        }

        ex_rate = 0;

        $("input[name='jiaxijuan']:radio").each(function() {

            if ($(this).is(":checked")) {
                ex_rate = parseFloat($(this).attr('value'));
            }
        });

        var money = true_money;
        changeShouyi(money);
    }

    // function getValue(name){

    //     var radio = document.getElementsByName(name);
    //     for (i=0; i<radio.length; i++) {
    //         if (radio[i].checked) {
    //             return radio[i].value;
    //         }
    //     }
    // }

    function selHuolifangshi(value) {

        var limit =1;

        if (value == 1) {
            rate = api.pageParam.rate;
        }
        if (value == 2) {
            rate = api.pageParam.rate_month;
            limit=parseFloat(time_limit);
        }
        all_rate = parseFloat(rate) + parseFloat(ex_rate);
        $('#shouyi').text('￥' + outputmoney(parseFloat(true_money) * parseFloat(all_rate.toFixed(2)) / 100 * parseFloat(time_limit) / 12 / limit ));
    }

    function addmoney() {

        var money = parseInt($('#money').text().replace(/,/g, ''));

        // 投资上限为空则为散标
        if (max_loan_money == null) {
            if (money + add_money * times > borrow_amount) {
                alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                $('#money').text(outputmoney(borrow_amount));
                true_money = borrow_amount;
                changeShouyi(borrow_amount);
                changeShifu();
                return;
            }
        } else {
            if (max_loan_money == 0) {
                if (money + add_money * times > borrow_amount) {
                    alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                    $('#money').text(outputmoney(borrow_amount));
                    true_money = borrow_amount;
                    changeShouyi(borrow_amount);
                    changeShifu();
                    return;
                }
            } else {
                if (money + add_money * times > borrow_amount) {
                    alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                    $('#money').text(outputmoney(borrow_amount));
                    true_money = borrow_amount;
                    changeShouyi(borrow_amount);
                    changeShifu();
                    return;
                }

                if (money + add_money * times > max_loan_money) {
                    alert('投资上限为' + outputmoney(max_loan_money) + '元');
                    $('#money').text(outputmoney(max_loan_money));
                    true_money = max_loan_money;
                    changeShouyi(max_loan_money);
                    changeShifu();
                    return;
                }
            }
        }

        money = money + add_money * times;
        $('#money').text(outputmoney(money));
        true_money = money;
        changeShouyi(money);
        changeShifu();
    }

    function reducemoney() {
        var money = parseInt($('#money').text().replace(/,/g, ''));
        if (min_money_format_num >= money) {
            alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
            $('#money').text(outputmoney(min_money_format_num));
            true_money = min_money_format_num;
            changeShouyi(min_money_format_num);
            changeShifu();
            return;
        }
        if (money > min_money_format_num * times) {
            money = money - add_money * times;
            $('#money').text(outputmoney(money));
            true_money = money;
            changeShouyi(money);
            changeShifu();
        }else {
            alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
            $('#money').text(outputmoney(min_money_format_num));
            true_money = min_money_format_num;
            changeShouyi(min_money_format_num);
            changeShifu();
            return;
        }
    }

    function cal() {
        api.closeWin({});
    }

    // 请求数据
    function updateData() {
        var userid = $api.getStorage('userid');
        api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center&act=rewardlist',
            method: 'post',
            data: {
                values: {
                    id: userid,
                    type: 0,
                    reward: 0,
                },
            }
        }, function(ret, err) {

            if (ret.status == 0) {
                alert(ret.info);
                return false;
            } else {
                var data = ret.reward;
                $api.setStorage('red_packet', data);
                if (data == '') {

                    var list = $api.byId('hongbaoList');
                    list.innerHTML = '没有可用的红包';

                } else {
                    templateFun(data);
                }
            }

        });
    }

    // 模板赋值函数
    function templateFun(data) {

        for (var i = 0; i < data.length; i++) {
            data[i].money = outputmoney(data[i].money);
        }
        var template = $api.byId('template');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var user = $api.byId('hongbaoList');
        $api.html(user, html);

    }

    // 请求数据
    function updateData2() {
        var userid = $api.getStorage('userid');
        api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center&act=rewardlist',
            method: 'post',
            data: {
                values: {
                    id: userid,
                    type: 0,
                    reward: 1,
                },
            }
        }, function(ret, err) {

            if (ret.status == 0) {
                alert(ret.info);
                return false;
            } else {
                var data = ret.reward;
                $api.setStorage('red_packet', data);
                if (data == '') {

                    var list = $api.byId('jiaxijuanList');
                    list.innerHTML = '没有可用的加息劵';

                } else {
                    templateFun2(data);
                }
            }

        });
    }

    // 模板赋值函数
    function templateFun2(data) {

        for (var i = 0; i < data.length; i++) {
            data[i].money = outputmoney(data[i].money);
        }
        var template = $api.byId('template2');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var user = $api.byId('jiaxijuanList');
        $api.html(user, html);

    }

    function xieyi1() {

    }

    function xieyi2() {

    }

    function xieyi3() {

    }

    function xieyi4() {

    }

    function buy() {

        api.confirm({
            title: '确认提醒',
            msg: '购买后,期限内资金将不可赎回.是否确认购买?',
            buttons: ['确定', '取消']
        }, function(ret, err){
            api.hideProgress();
            if( ret ){
                var index = ret.buttonIndex;
                if (index == 1) {
                    pay_alert(); 
                }
            }else{
               alert( JSON.stringify( err ) );
           }
       });
    }


    //重新加载
    function reload() {


      api.closeWin({
          name: 'ios-unlockBuy'
      });
      api.toast({
          msg: '解锁失败',
          duration: 2000,
          location: 'middle'
      });

      setTimeout("opeanReload()", 400);


  }

  function opeanReload() {
      api.openWin({
          name: 'ios-unlockBuy',
          url: './ios-unlockBuy.html',
          bgColor:'#00744b',
          slidBackEnabled:false,
          pageParam:{
            login:'licai-buy'
        },
    });

  }

  function pay_alert(){
    if (api.systemType == 'ios') {
        var password = $api.getStorage('password');
        if (password) {
          api.openWin({
              name: 'ios-unlockBuy',
              url: './ios-unlockBuy.html',
              bgColor:'#00744b',
              pageParam:{
                login:'licai-buy'
            },
            slidBackEnabled:false,
        });

      } else {
        alert('请先添加手势密码');
    }

} else {
    var isLock = $api.getStorage('isLock');
    if (isLock) {
        var screenLock = api.require('screenLock');
        screenLock.show({
            color: '#00744b'
        }, function(ret, err) {
            $api.setStorage('buy', true);
            if (ret.status == '111') {
                api.showProgress({
                    style: 'default',
                    animationType: 'fade',
                    title: '努力加载中...',
                    text: '先喝杯茶...',
                    modal: false
                });

                pay();
            }
        });
    } else {
        alert('请先添加手势密码');
    }
}
}

function pay() {
    var userid = $api.getStorage('userid');

    var ecv_id = '';
    $("input[name='hongbao']:checkbox").each(function() {

        if ($(this).is(":checked")) {
            ecv_id += ',' + $(this).attr('hongbaoID');
        }
    });
    ecv_id = ecv_id.substr(1, ecv_id.length - 1);

    if (ex_rate == 0) {
        var use_interestrate = 0;
    } else {
        var use_interestrate = 1;
    }

    var interestrate_id = '';
    $("input[name='jiaxijuan']:radio").each(function() {

        if ($(this).is(":checked")) {
            interestrate_id = $(this).attr('jiaxijuanID');
        }
    });

    var hl_type = '';
    var radio = document.getElementsByName('huolifangshi');
    for (i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            var type = radio[i].value;
            if (type == 1) {
                hl_type = 0;
            }
            if (type == 2) {
                hl_type = 1;
            }
        }
    }

    var url = '';
        // 投资上限为空则为散标
        if (max_loan_money == null) {
            url = webAddress + '/index.php?ctl=deal&act=dobid1';
            api.ajax({
                url: url,
                method: 'post',
                data: {
                    values: {
                        ajax: '1',
                        agent: 'h5',
                        userid: userid,
                        id: id,
                        money: true_money,
                        ecv_id: ecv_id,
                        use_interestrate: use_interestrate,
                        interestrate_id: interestrate_id,
                        hl_type: hl_type,
                    }
                }
            }, function(ret, err) {
                if (ret) {

                    if (ret.status == 0) {

                        alert(ret.info);
                    } else {

                        // api.ajax({
                        //     url: webAddress + ret.jump,
                        //     method: 'get',
                        //     data: {
                        //         values: {
                        //             userid: userid,
                        //         }
                        //     }
                        // }, function(ret, err) {

                        //     if (ret) {

                        //         if (ret.status == 0) {
                        //             alert(ret.info);
                        //             return;
                        //         }

                        //         // api.toast({
                        //         //     msg: '购买成功',
                        //         //     duration: 2000,
                        //         //     location: 'middle'
                        //         // });

                        //         api.alert({
                        //             title: '恭喜您!您的智能选投已经购买成功!',
                        //             msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        //         }, function(ret, err) {
                        //             api.openWin({
                        //                 name: 'loan-list',
                        //                 url: './loan-list.html',
                        //                 pageParam: {
                        //                     toubiao: 'toubiao'
                        //                 }
                        //             });

                        //         });



                        //     } else {
                        //         alert(err.msg);
                        //     }
                        // });


                        // api.alert({
                        //     title: '恭喜您!您的智能选投已经购买成功!',
                        //     msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        // }, function(ret, err) {
                        //     api.openWin({
                        //         name: 'loan-list',
                        //         url: './loan-list.html',
                        //         pageParam: {
                        //             toubiao: 'toubiao'
                        //         }
                        //     });

                        // });

                        var delay = 0;
                        if (api.systemType != 'ios') {
                            // delay = 300;
                        }


                        api.alert({
                            title: '恭喜您!您的智能选投已经购买成功!',
                            msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        }, function(ret, err) {
                            api.openWin({
                                name: 'my_fund',
                                url: './my-fund.html',
                                bounces: false,
                                delay: delay,
                                slidBackEnabled: true,
                                vScrollBarEnabled: false,
                                progress: {
                                    type: "page"
                                }
                            });

                        });

                    }
                } else {
                    alert(err.msg);
                }
            });
} else {
    url = webAddress + '/index.php?ctl=deal&act=dobid';
    api.ajax({
        url: url,
        method: 'post',
        data: {
            values: {
                ajax: '1',
                agent: 'h5',
                userid: userid,
                id: id,
                bid_money: true_money,
                ecv_id: ecv_id,
                use_interestrate: use_interestrate,
                interestrate_id: interestrate_id,
                hl_type: hl_type,
            }
        }
    }, function(ret, err) {
        if (ret) {

            if (ret.status == 0) {

                alert(ret.info);
            } else {
                        // api.ajax({
                        //     url: webAddress + ret.jump,
                        //     method: 'get',
                        //     data: {
                        //         values: {
                        //             userid: userid,
                        //         }
                        //     }
                        // }, function(ret, err) {

                        //     if (ret) {

                        //         if (ret.status == 0) {
                        //             alert(ret.info);
                        //             return;
                        //         }

                        //         // api.toast({
                        //         //     msg: '购买成功',
                        //         //     duration: 2000,
                        //         //     location: 'middle'
                        //         // });
                        //         api.alert({
                        //             title: '恭喜您!您的智能选投已经购买成功!',
                        //             msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        //         }, function(ret, err) {
                        //             api.openWin({
                        //                 name: 'loan-list',
                        //                 url: './loan-list.html',
                        //                 pageParam: {
                        //                     toubiao: 'toubiao'
                        //                 }
                        //             });

                        //         });


                        //     } else {
                        //         alert(err.msg);
                        //     }
                        // });
                        

                        // api.alert({
                        //     title: '恭喜您!您的智能选投已经购买成功!',
                        //     msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        // }, function(ret, err) {
                        //     api.openWin({
                        //         name: 'loan-list',
                        //         url: './loan-list.html',
                        //         pageParam: {
                        //             toubiao: 'toubiao'
                        //         }
                        //     });

                        // });

                        var delay = 0;
                        if (api.systemType != 'ios') {
                            // delay = 300;
                        }
                        
                        api.alert({
                            title: '恭喜您!您的智能选投已经购买成功!',
                            msg: '我们将在7-15个工作日为您匹配合适的债权产品。匹配成功后,您可在我的账户中查看债权详情及电子合同。',
                        }, function(ret, err) {
                            api.openWin({
                                name: 'my_fund',
                                url: './my-fund.html',
                                bounces: false,
                                delay: delay,
                                slidBackEnabled: true,
                                vScrollBarEnabled: false,
                                progress: {
                                    type: "page"
                                }
                            });

                        });

                    }
                } else {
                    alert(err.msg);
                }
            });
}
api.hideProgress();
}
</script>

</html>
